﻿// --------------------------------
// <copyright file="ConstantEntryForm.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using Tronsoft.Actucal.CalculationEngineCreator;
using WeifenLuo.WinFormsUI.Docking;

namespace Actucal
{
    public partial class ConstantEntryForm : DockContent
    {
        public event EventHandler<ArgumentEventArgs<string>> CurrentConstantChanged;

        public ConstantEntryForm()
        {
            InitializeComponent();

            ProductsService.CurrentProductChanged += ProductsService_CurrentProductChanged;
            AddConstantForm.ConstantAdded += AddConstantForm_ConstantAdded;
        }

        protected virtual void OnCurrentConstantChanged(ArgumentEventArgs<string> e)
        {
            if (null != CurrentConstantChanged)
                CurrentConstantChanged(this, e);
        }

        private void AddConstantForm_ConstantAdded(object sender, EventArgs e)
        {
            AddConstantForm frm = sender as AddConstantForm;
            if (null != sender)
            {
                lbConstants.Items.Add(frm.Constant.Name);
            }
        }

        private void ProductsService_CurrentProductChanged(object sender, ArgumentEventArgs<Product> e)
        {
            LoadConstants(e.Argument);
        }

        private void miDelete_Click(object sender, EventArgs e)
        {
            if (lbConstants.SelectedIndex != -1)
            {
                string name = lbConstants.SelectedItem.ToString();
                ProductsService.CurrentProduct.Constants.RemoveAll(delegate(INameValueCombination constant)
                                                                       {
                                                                           return constant.Name == name;
                                                                       });
                lbConstants.Items.RemoveAt(lbConstants.SelectedIndex);
            }
        }

        private void LoadConstants(Product product)
        {
            lbConstants.Items.Clear();
            if ((product != null ) && (product.Constants != null))
            {
                foreach (INameValueCombination constant in product.Constants)
                {
                    lbConstants.Items.Add(constant.Name);
                }
            }
        }

        private void ConstantEntryForm_Load(object sender, EventArgs e)
        {
            LoadConstants(ProductsService.CurrentProduct);
        }

        private void lbConstants_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnCurrentConstantChanged(new ArgumentEventArgs<string>(lbConstants.SelectedItem.ToString()));
        }
    }
}